package com.jQueryAjax.controller;

import java.util.Date;
import java.util.List;

import com.jQueryAjax.model.Message;
import com.jfinal.core.Controller;

public class MessageController extends Controller {

	public void getMsg(){
		List<Message> messages = Message.dao.find("select * from message");
		long servTimeStamp = messages.get(messages.size()-1).getDate("time").getTime();
		System.out.println("getParaToLong('time'):"+getParaToLong("time"));
		long clientTimeStamp = Long.parseLong(getPara("time"));
		if(!(servTimeStamp == clientTimeStamp)){
			renderText(xmlStr());
		}else{
			renderText("<chatRoom><status>2</status></chatRoom>");
		}
		
	}
	public String xmlStr(){
		List<Message> messages = Message.dao.find("select * from message");
		System.out.println("查到表中有"+messages.size()+"条聊天记录");
		
		String str = "<chatRoom><status>1</status><timeStamp>"+messages.get(messages.size()-1).getDate("time").getTime()+"</timeStamp>";
		for (Message message : messages) {
			str += "<msg><author>"+message.getStr("author")+"</author>" +
            "<content>"+message.getStr("content")+"</content></msg>";
		}
		return str+"</chatRoom>";
	}
	public void ajaxForm(){
//		String name = getPara("name");
//		String content = getPara("content");
		Message msg = getModel(Message.class,"message");
		msg.set("time", new Date());
		msg.save();
		renderText(xmlStr());
//		renderText("<chatRoom><status>1</status><timeStamp></timeStamp><msg><author>"+name+"</author>" +
//	            "<content>"+content+"</content></msg></chatRoom>");
	}
}
